var UtilizationInfo = function (res) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (res) {
      this.setRateData(res);
      self.monthChartData = {};
      self.dayChartData = {};
      self.dayChartIndex = 0;
      self.equipmentList = [];
    },

    setRateData: function (res) {
      this.setMonthRateData(res.monthRateData);
      this.setDayRateData(res.dayRateData);
    },

    setMonthRateData: function (rateData) {
      self.monthRateId = SysUtil.getId();
      var monthRateList = rateData.monthRateList;
      var monthRateObj = _.groupBy(monthRateList, 'equipmentNo');
      self.year = rateData.year;
      self.monthRateObj = monthRateObj;
    },

    setDayRateData: function (rateData) {
      self.dayRateId = SysUtil.getId();
      var dayRateList = rateData.dayRateList;
      var dayRateObj = _.groupBy(dayRateList, 'equipmentNo');

      var month = rateData.month;
      var dayCount;
      if (moment().isSame(month, 'month')) {
        dayCount = moment().get('date');
      } else {
        dayCount = moment(month).daysInMonth();
      }
      self.month = month;
      self.dayCount = dayCount;
      self.dayRateObj = dayRateObj;
    },

    setEquipmentList: function (deptWork) {
      var monthRateObj = self.monthRateObj;
      var equipmentList = [];
      if (deptWork === null) {
        equipmentList = _.keys(monthRateObj);
      } else {
        for (var equipmentNo in monthRateObj) {
          var list = monthRateObj[equipmentNo];
          var obj = list[0];
          if (this.checkDeptWorkValid(obj, deptWork)) {
            equipmentList.push(equipmentNo);
          }
        }
      }

      self.equipmentList = equipmentList;
    },

    checkDeptWorkValid: function (obj, deptWork) {
      var dept = deptWork.dept;
      if (dept !== undefined) {
        if (obj.deptId !== dept) {
          return false;
        }
      }

      var workshop = deptWork.workshop;
      if (workshop !== undefined) {
        if (obj.workShopId !== workshop) {
          return false;
        }
      }
      return true;
    },

    formatMonthRateData: function (monthRateList) {
      var year = self.year;
      var monthCount = 12;
      if (moment().isSame(year, 'year')) {
        monthCount = moment().get('month') + 1;
      }

      year = moment(year).format('YYYY');
      var data = {};
      for (var i = 1; i <= monthCount; i++) {
        var month = _.padStart(i, 2, '0');
        month = `${year}-${month}`;
        data[month] = {
          month: month,
          value: 0
        };
      }

      var maxValue = 0;
      for (var i = 0; i < monthRateList.length; i++) {
        var monthRate = monthRateList[i];
        var value = this.getRateValue(monthRate, 'month');
        data[monthRate.timeName].value = value;
        if (maxValue < value) {
          maxValue = value;
        }
      }
      return {
        maxValue: maxValue,
        data: _.values(data)
      };
    },

    formatDayRateData: function (dayRateList) {
      var month = self.month;
      var dayCount = self.dayCount;

      month = moment(month).format('YYYY-MM');
      var data = {};
      for (var i = 1; i <= dayCount; i++) {
        var day = _.padStart(i, 2, '0');
        day = `${month}-${day}`;
        data[day] = {
          day: day,
          value: 0
        };
      }

      var maxValue = 0;
      for (var i = 0; i < dayRateList.length; i++) {
        var dayRate = dayRateList[i];
        var value = this.getRateValue(dayRate, 'day');
        data[dayRate.timeName].value = value;
        if (maxValue < value) {
          maxValue = value;
        }
      }

      return {
        maxValue: maxValue,
        data: _.values(data)
      };
    },

    getRateValue: function (rateObj, type) {
      var total = rateObj.totalTimeOfDay;
      var hour = rateObj.dailyWorkingHours;
      if (total === 0 || hour === 0) {
        return 0;
      }

      var value;
      if (type === 'month') {
        var timeName = rateObj.timeName;
        var days;
        if (moment(timeName, 'YYYY-MM').isSame(moment(), 'month')) {
          days = moment().get('date');
        } else {
          days = moment(timeName, 'YYYY-MM').daysInMonth();
        }
        value = total / (hour * 3600 * days) * 100;
      } else if (type === 'day') {
        value = total / (hour * 3600) * 100;
      }

      value = _.round(value, 2);
      return value;
    },

    pageDayRateData: function (dayRateData) {
      var data = dayRateData.data;
      var dayChartIndex = self.dayChartIndex;
      dayRateData.data = data.slice(dayChartIndex, dayChartIndex + 12);
      return dayRateData;
    }
  };

  this.incrementDayIndex = function () {
    var dayCount = this.dayCount;
    var perSize = 12;
    if (dayCount <= perSize) {
      return false;
    }

    dayCount = dayCount - perSize + 1;
    var dayChartIndex = this.dayChartIndex;
    this.dayChartIndex = (dayChartIndex + 1) % dayCount;
    return true;
  };

  this.getMonthChartData = function (pagination) {
    var currentPage = pagination.currentPage;
    var pageSize = pagination.pageSize;
    var startIndex = (currentPage - 1) * pageSize;

    var equipmentList = this.equipmentList;
    equipmentList = equipmentList.slice(startIndex, startIndex + pageSize);
    var monthRateObj = this.monthRateObj;

    var monthChartData = self.monthChartData;
    var data = [];
    for (var i = 0; i < equipmentList.length; i++) {
      var equipmentNo = equipmentList[i];
      var monthRateData = monthChartData[equipmentNo];
      if (monthRateData === undefined) {
        var monthRate = monthRateObj[equipmentNo];
        monthRateData = fn.formatMonthRateData(monthRate);
        monthRateData.equipmentNo = equipmentNo;
        monthChartData[equipmentNo] = monthRateData;
      }
      data.push(monthRateData);
    }
    return data;
  };

  this.getDayChartData = function (pagination) {
    var currentPage = pagination.currentPage;
    var pageSize = pagination.pageSize;
    var startIndex = (currentPage - 1) * pageSize;

    var equipmentList = this.equipmentList;
    equipmentList = equipmentList.slice(startIndex, startIndex + pageSize);
    var dayRateObj = this.dayRateObj;

    var dayChartData = self.dayChartData;
    var data = [];
    for (var i = 0; i < equipmentList.length; i++) {
      var equipmentNo = equipmentList[i];
      var dayRateData = dayChartData[equipmentNo];
      if (dayRateData === undefined) {
        var dayRate = dayRateObj[equipmentNo];
        dayRateData = fn.formatDayRateData(dayRate);
        dayRateData.equipmentNo = equipmentNo;
        dayChartData[equipmentNo] = dayRateData;
      }

      dayRateData = _.cloneDeep(dayRateData);
      dayRateData = fn.pageDayRateData(dayRateData);
      data.push(dayRateData);
    }
    return data;
  };

  this.resetObject = function (res) {
    var monthRateData = res.monthRateData;
    if (!SysUtil.isEmpty(monthRateData)) {
      fn.setMonthRateData(monthRateData);
      this.monthChartData = {};
    }

    var dayRateData = res.dayRateData;
    if (!SysUtil.isEmpty(dayRateData)) {
      fn.setDayRateData(dayRateData);
      this.dayChartData = {};
      this.dayChartIndex = 0;
    }
  };

  this.searchData = function (deptWork) {
    fn.setEquipmentList(deptWork);
    this.dayChartIndex = 0;
  };

  this.getDeptWorkList = function () {
    var monthRateObj = this.monthRateObj;
    var deptWorkshopMap = {};
    for (var key in monthRateObj) {
      var monthRate = monthRateObj[key][0];
      var deptId = monthRate.deptId;
      var deptWorkshop = deptWorkshopMap[deptId];
      if (SysUtil.isEmpty(deptId) || deptId==='0') {
        continue;
      }
      if (deptWorkshop === undefined) {
        deptWorkshop = {
          id: `dept-${deptId}`,
          name: monthRate.deptName,
          workshops: []
        };
        deptWorkshopMap[deptId] = deptWorkshop;
      }

      var workShopId = monthRate.workShopId;
      if (SysUtil.isEmpty(workShopId) || workShopId==='0') {
        continue;
      }
      deptWorkshop.workshops.push({
        id: `workshop-${workShopId}`,
        name: monthRate.workShopName
      });
    }

    var deptWorkshopList = [];
    for (var deptId in deptWorkshopMap) {
      var deptWorkshop = deptWorkshopMap[deptId];
      deptWorkshop.workshops = _.uniqBy(deptWorkshop.workshops, 'id');
      deptWorkshopList.push(deptWorkshop);
    }
    return deptWorkshopList;
  };

  fn.initData(res);
};
